package com.zhiqian.community.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.UUID;

public class CommunityUtil {
    //生成随机字符串
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    //MD5加密
    public static String MD5(String key){
        if (StringUtils.isBlank(key)){
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }

    public static String getJSONString(int code, String msg, Map<String,Object> map){
        JSONObject object = new JSONObject();
        object.put("code",code);
        if (!StringUtils.isBlank(msg)){
            object.put("msg",msg);
        }
        if (map!=null&&!map.isEmpty()){
            for (Map.Entry<String, Object> entry:map.entrySet()){
                object.put(entry.getKey(),entry.getValue());
            }
        }
        return object.toJSONString();
    }

    public static String getJSONString(int code, Map<String,Object> map){
       return getJSONString(code,null,map);
    }

    public static String getJSONString(int code, String msg){
        return getJSONString(code,msg,null);
    }
}
